package com.wwwarehouse.taskcenter.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.wwwarehouse.common.custom_widget.CircleImageView;
import com.wwwarehouse.common.tools.ImageloaderUtils;
import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.bean.InviterRankBean;

import java.util.List;

/**
 * @创建者 chenchao.
 * @创建时间 2017/10/9 20:31
 * @描述 ${TODO}
 */

public class SelectInviterChildAdapter extends BaseAdapter {


    private Context                        mContext;
    private List<InviterRankBean.ListBean> mInviteDetailsList;
    private GridView                       mGridView;
    private ImageLoader                    mImageLoader;
    private DisplayImageOptions            mImageOptions;

    @SuppressWarnings("ResourceType")
    public  SelectInviterChildAdapter(Context context, List<InviterRankBean.ListBean> inviteDetailsList, GridView gridView) {
        this.mContext = context;
        this.mInviteDetailsList = inviteDetailsList;
        this.mGridView = gridView;

        mImageLoader = ImageloaderUtils.getImageLoader();
        mImageOptions = ImageloaderUtils.initImageLoaderOption(R.drawable.invite_head, R.drawable.invite_head);

    }

    @Override
    public int getCount() {
        return mInviteDetailsList == null ? 0 : mInviteDetailsList.size();
    }

    @Override
    public Object getItem(int position) {
        return mInviteDetailsList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final InviterRankBean.ListBean listBean = mInviteDetailsList.get(position);
        ViewHolder holder;

        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.select_invite_child_item, null);
            holder = new ViewHolder();
            holder.faceImg = (CircleImageView) convertView.findViewById(R.id.iv_face);
            holder.nameTxt = (TextView) convertView.findViewById(R.id.tv_name);
            convertView.setTag(holder);


        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final View finalConvertView = convertView;
        mGridView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                AbsListView.LayoutParams params =  new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                int itemHeight = (mGridView.getMeasuredHeight() - mGridView.getPaddingTop() - mGridView.getPaddingBottom() - mGridView.getVerticalSpacing() * (mGridView.getNumColumns() - 1)) / mGridView.getNumColumns();
                params.height = itemHeight;
                finalConvertView.setLayoutParams(params);
                mGridView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });
        mImageLoader.displayImage(listBean.getInviterUrl(), holder.faceImg, mImageOptions, new ImageloaderUtils.AnimateFirstDisplayListener());
        holder.nameTxt.setText(listBean.getInviterName());
        return convertView;
    }

    private class ViewHolder {
        private CircleImageView faceImg;
        private TextView        nameTxt;
    }
}
